package api.doctor;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONException;

import common.Config;
import common.Host;
import common.Request;

/**
 * 实现帮忙医医生端聊天模板IMChatTemplate接口调用
 * @author gaowen
 *
 */
public class IMChatTemplate
{
	/**
	 * 实现帮忙医医生端获取聊天模板接口IMChatTemplate接口调用
	 * @param token 掉接口需要的token
	 * @return 接口返回值
	 * @throws IOException
	 */
	public static String get(String token) throws IOException
	{
		String url=Host.getAddress()+"/api/IMChatTemplate";
		
		String result=Request.get(url, null,token);
		return result;
	}
	
	/**
	 * 实现帮忙医医生端编辑聊天模板接口IMChatTemplate接口调用
	 * @param ID 模板ID
	 * @param Section 科室
	 * @param TemplateName 模板名
	 * @param TemplateContent 模板内容
	 * @param token 调接口需要的token
	 * @return 接口返回值
	 * @throws JSONException
	 * @throws IOException
	 */
	public static String post(String ID,String Section,String TemplateName,String TemplateContent,String token) throws JSONException, IOException
	{
		String url=Host.getAddress()+"/api/IMChatTemplate";
		
		String parms="={ ID: "+ID+", Section: '"+Section+"', TemplateName: '"+TemplateName+"', TemplateContent: '"+TemplateContent+"' }";
		
		String result=post(url, parms, token);
		return result;
	}
	
	/**
	 * 实现带token的Post接口调用
	 * @param url 地址
	 * @param content 传输内容
	 * @param token 调接口需要的token
	 * @return
	 * @throws IOException
	 */
	private static String post(String url, String content, String token) throws IOException
	{
		// 建立HTTP连接
		URL postUrl = new URL(url);
		HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
		connection.setRequestMethod("POST");
		connection.setDoInput(true);
		connection.setDoOutput(true);

		// 设置HTTP-Headers：Content-Type
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

		// 设置HTTP-Headers：Token
		if (token != null)
		{
			connection.setRequestProperty("Token", token);
		}

		// 设置超时间
		int time = Integer.parseInt(Config.getValue("Timeout", "time"));
		connection.setConnectTimeout(time * 1000);
		connection.setReadTimeout(time * 1000);

		// 设置HTTP-Content
		if (content != null)
		{
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			out.write(content.getBytes("UTF-8"));
			out.flush();
			out.close();
		}
		else
		{
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			out.write("".getBytes("UTF-8"));
			out.flush();
			out.close();
		}

		// 调用接口，并将接口返回数据转换成String类型
		String result = null;
		try
		{
			result = convertStreamToString(connection.getInputStream());
		}
		catch (IOException e)
		{
			// 接口调用失败，返回null
			// e.printStackTrace();
			return null;
		}

		// 关闭HTTP连接
		connection.disconnect();

		return result;
	}
	
	/**
	 * 实现InputStream转换String
	 * 
	 * @param inputStream
	 *            - 需要转换的InputStream
	 * @return 转换后的String；如何转换失败则返回null
	 * @throws IOException
	 */
	private static String convertStreamToString(InputStream inputStream) throws IOException
	{
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		StringBuffer sb = new StringBuffer();
		String Line = null;
		try
		{
			while ((Line = reader.readLine()) != null)
			{
				sb.append(Line);
			}
		}
		catch (IOException e)
		{
			// 转换失败，返回null
			reader.close();
			return null;
		}

		reader.close();
		// String ret = new String(sb.toString().getBytes("GBK"), "UTF-8");
		String ret = sb.toString();
		return ret;
	}
}
